package com.familydoctor.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.familydoctor.entity.Category;
import com.familydoctor.entity.ContentSheetModel;
import com.familydoctor.entity.DetailModel;
import com.familydoctor.init.CategoryCache;
import com.familydoctor.util.StringCustomUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;


/**
 * @program: tool
 * @description: exl处理
 * @author: tmz
 * @create: 2020-10-10 16:45
 */
public class MyExlListener extends AnalysisEventListener<ContentSheetModel> {
    private List<DetailModel> res = new CopyOnWriteArrayList<>();
    private List<Category> category = new ArrayList<>();
    private Map<String,Category> map = new HashMap<>();
    public MyExlListener(){
        init();
    }
    public MyExlListener(List<DetailModel> res,List<Category> category){
        this.res = res;
        this.category = category;
    }
    @Override
    public void invoke(ContentSheetModel data, AnalysisContext context) {
        DetailModel detailModel = new DetailModel();
        detailModel.setCategory(data.getType())
                .setCompleted(StringCustomUtils.getNumByStr(data.getFinalPeople())*10000)
                .setEstimate(StringCustomUtils.getNumByStr(data.getElapsedTime()))
                .setQuestions(StringCustomUtils.getNumByStr(data.getQuestions()))
                .setTitle(data.getTitle()).setSummary(data.getSummary());
        res.add(detailModel);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {}
    private void init(){
        category = new CategoryCache().getCategory();
        category.forEach(item->map.put(item.getName(),item));
    }
}
